﻿using System.Data;
using System.Data.SqlClient;

namespace Nardax.Data
{
	public static class SqlParameterCollectionExtensions
	{
		/// <summary> 
		/// Adds a parameter using the parameter name and the direction of the parameter. 
		/// </summary> 
		/// <Remarks>The parameters in this method are a little out of order for a very specific reason. If you put parameter direction 
		/// after SqlDbType the compiler thinks that parameter direction is an int and uses your parameter direction become size and this
		/// extension method doesn't work.</Remarks> 
		public static SqlParameter Add(this SqlParameterCollection parameters, string parameterName, SqlDbType sqlDbType, ParameterDirection parameterDirection)
		{
			var parameter = parameters.Add(parameterName, sqlDbType);
			parameter.Direction = parameterDirection;
			return parameter;
		}

		public static SqlParameter Add(this SqlParameterCollection parameters, string parameterName, SqlDbType sqlDbType, int size, ParameterDirection parameterDirection)
		{
			var parameter = parameters.Add(parameterName, sqlDbType, size);
			parameter.Direction = parameterDirection;
			return parameter;
		}

		public static SqlParameter Add(this SqlParameterCollection parameters, string parameterName, SqlDbType sqlDbType, int size, string sourceColumn, ParameterDirection parameterDirection)
		{
			var parameter = parameters.Add(parameterName, sqlDbType, size, sourceColumn);
			parameter.Direction = parameterDirection;
			return parameter;
		}

		public static SqlParameter AddWithValue(this SqlParameterCollection parameters, string parameterName, object value, ParameterDirection parameterDirection)
		{
			var parameter = parameters.AddWithValue(parameterName, value);
			parameter.Direction = parameterDirection;
			return parameter;
		}

		public static SqlParameter AddWithValue(this SqlParameterCollection parameters, string parameterName, object value, SqlDbType sqlDbType)
		{
			var parameter = parameters.AddWithValue(parameterName, value);
			parameter.SqlDbType = sqlDbType;
			return parameter;
		}

		public static SqlParameter AddWithValue(this SqlParameterCollection parameters, string parameterName, object value, SqlDbType sqlDbType, int size)
		{
			var parameter = parameters.AddWithValue(parameterName, value);
			parameter.SqlDbType = sqlDbType;
			parameter.Size = size;
			return parameter;
		}

		public static SqlParameter AddWithValue(this SqlParameterCollection parameters, string parameterName, object value, SqlDbType sqlDbType, int size, ParameterDirection parameterDirection)
		{
			var parameter = parameters.AddWithValue(parameterName, value);
			parameter.SqlDbType = sqlDbType;
			parameter.Size = size;
			parameter.Direction = parameterDirection;
			return parameter;
		}
	}
}
